﻿
''' <summary>
''' セットアップページ
''' </summary>
''' <remarks></remarks>
Public Class Setup
	Inherits UI.CommonPage

#Region " Declare "

	''' <summary>入力された内容の退避</summary>
	Private _values As SetupEntity

#End Region

#Region " Handles "

	''' <summary>
	''' 初期化
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub Setup_Init(sender As Object, e As EventArgs) Handles Me.Init
		validator = New Moca.Util.Validator
	End Sub

	''' <summary>
	''' ロード
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Sys.Settings.IsSetup Then
			Return
		End If

		Response.Redirect("~/Index.aspx")
	End Sub

	''' <summary>
	''' 検証
	''' </summary>
	''' <param name="source"></param>
	''' <param name="args"></param>
	''' <remarks></remarks>
	Private Sub validMain_ServerValidate(source As Object, args As ServerValidateEventArgs) Handles validMain.ServerValidate
		Dim webCtrl() As WebControl = Nothing
		Dim value As New SetupEntity

		' 基本的な検証とエンティティへの退避
		args.IsValid = UpdateEntity(Me, value, AddressOf _validUpdateEntity)

		If Not args.IsValid Then
			Return
		End If

		_values = value

		args.IsValid = True
	End Sub

	''' <summary>
	''' セットアップボタンクリック
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Sub btnSetup_Click(sender As Object, e As System.EventArgs) Handles btnSetup.Click
		If Not Me.IsValid Then
			Return
		End If

		Try
			Cryptor.CreateKeys()

			daoCommon.Setup(Me.txtMail.Text, Me.txtPassword.Text)
		Catch ex As Exception
			Cryptor.DeleteKeys()
			Throw ex
		End Try

		Response.Redirect("~/Index.aspx")
	End Sub

#End Region
#Region " Methods "

	''' <summary>
	''' 検証
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks>
	''' 相関チェックなど追加のチェックを行う
	''' </remarks>
	Private Sub _validUpdateEntity(ByVal sender As Object, ByVal e As Moca.UpdateEntityValidateArgs)
		Dim webCtrl() As WebControl = New WebControl() {sender}

		' メッセージ設定
		setErrorMessage(e.Caption, Me.validMain, webCtrl, e.Value, e.ValidateResultType, e.Min, e.Max)
	End Sub

#End Region

End Class